import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { FetchService } from '../../../../../core/net/fetch.service';
import * as moment from 'moment';

const stationHash = {
    '168': '跃进渠一所',
    '169': '跃进渠二所'
};

@Component({
    selector: 'app-gt-control-section',
    templateUrl: './gt-control-section.component.html',
    styleUrls: ['../gate-control-list.component.scss', './gt-control-section.component.scss']
})
export class GtControlSectionComponent implements OnInit {
    tableData = [];
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = false;
    searchForm: FormGroup;
    modalShow = false;
    stationId = null;
    historyTableData = [];
    historyForm: FormGroup;
    constructor(private router: Router,
                private fb: FormBuilder,
                private fetchService: FetchService) {
        this.searchForm = this.fb.group({
            sectionName: [],
            sectionId: []
        });
        this.historyForm = this.fb.group({
            rangePickerTime: [[moment().subtract(7, 'days').valueOf(), new Date()]]
        });
    }

    ngOnInit(): void {
        //  查询管理段
        this.fetchService.post<any>('sys/sysDictValue/list', { dictTypeId: '1022' })
            .subscribe(res => {

            });
        this.queryInfo();
    }

    queryInfo(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.loading = true;
        const params = {
            ...this.searchForm.value,
            pageNum: this.pageNum,
            pageSize: this.pageSize
        };
        this.fetchService.post<Array<any>>('wrs/wrsBrcrossSection/info', params).subscribe(res => {
            const list = res || [];
            // const data = list.filter(item => {
            //     return !!item.info;
            // });
            this.tableData = list.map(row => {
                return {
                    ...row,
                    stName: stationHash[row.sectionId]
                };
            });
            this.total = res['total'];
            this.loading = false;
        });
    }

    directToSketchMap(): void {
        this.router.navigate(['/gate/gt-control']);
    }

    openModal(data) {
        this.modalShow = true;
        this.stationId = data.stationID;
        this.queryHistoryInfo();
    }
    queryHistoryInfo() {
        this.fetchService.post<Array<any>>('wrs/wrsBrcrossSection/queryHistory', {
            beginDate: moment(this.historyForm.value.rangePickerTime[0]).format('YYYY-MM-DD'),
            endDate: moment(this.historyForm.value.rangePickerTime[1]).format('YYYY-MM-DD'),
            stationId: this.stationId
        }).subscribe(res => {
            this.historyTableData = res;
        });
    }
    exportFile() {
        this.fetchService.export('wrs/wrsBrcrossSection/export', {
            beginDate: moment(this.historyForm.value.rangePickerTime[0]).format('YYYY-MM-DD'),
            endDate: moment(this.historyForm.value.rangePickerTime[1]).format('YYYY-MM-DD'),
            stationId: this.stationId
        }).subscribe(res => {
            const blob = new Blob([res], {type: 'application/vnd.ms-excel    application/x-excel;charset=utf-8'});
            const elink = document.createElement('a');

            elink.download = `历史记录`;
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
        });
    }

    onCancel() {
        this.modalShow = false;
    }
}
